module.exports = {
	async check_permission(config) {
		let _this = this;
		let { k, show_Modal = true, suc_fun = false, fail_fun = false } = config;
		let { permission_config } = this.COMMON_VAR;
		let permission_data = permission_config[k];
		permission_data['show_Modal'] = show_Modal;
		let _flag = true;
		let _check = await _this.check_permission_data(permission_data);
		// console.log(_check);
		if (!_check['WeChat_on']) {
			if (show_Modal) {
				_flag = await _this.com_modal({
					title: `权限申请`,
					content: permission_data.WeChat_content,
				});
				if (_flag) {
					uni.openSetting();
				} else {
					_this.com_msg(permission_data.WeChat_fail_tips);
					// fail_fun && fail_fun();
				}
				return;
			} else {
				_flag = false;
			}
		}
		if (!_check['system_on']) {
			if (show_Modal) {
				_flag = await _this.com_modal({
					title: `权限申请`,
					content: permission_data.system_tips,
				});
				if (_flag) {
					uni.openAppAuthorizeSetting();
				} else {
					// _this.com_msg(permission_data.WeChat_fail_tips);
					// fail_fun && fail_fun();
				}
				return;
			} else {
				_flag = false;
			}
		}

		if (_flag) {
			suc_fun && suc_fun();
		} else {
			fail_fun && fail_fun();
		}
	},
	async check_permission_data(data) {
		let _this = this;
		let my_system_config = uni.getAppAuthorizeSetting();
		let WeChat_setting = await _this.back_setting();
		// console.log({
		// 	my_system_config,
		// 	WeChat_setting,
		// });
		let { system, WeChat, system_platform, show_Modal } = data;
		let _back = {};

		if (_this.objJudge(system_platform)) {
			if (_this.COMMON_VAR.platform == system_platform) {
				// ios才能设置微信的蓝牙开启
				_back['system_on'] = my_system_config[system] == 'denied' ? false : true;
			} else {
				_back['system_on'] = true;
			}
		} else {
			_back['system_on'] = my_system_config[system] == 'denied' ? false : true;
		}

		// 主动触发和被动触发的判断条件不一样

		if (show_Modal) {
			// 主动触发可能会有没初始化过第一次的情况
			// WeChat_setting[WeChat] 会为空 所以此时需要允许初始化一次
			_back['WeChat_on'] = WeChat_setting[WeChat] == false ? false : true;
		} else {
			// 被动触发直接判断false或者true即可
			_back['WeChat_on'] = _this.objJudge(WeChat_setting[WeChat]) ? true : false;
		}

		if (WeChat == 'scope.userLocation') {
			// 不重复获取微信位置授权
			_back['WeChat_on'] = true;
		}
		return _back;
	},
};
